package com.example.administrator.mynews.fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.mynews.R;
import com.example.administrator.mynews.activity.GuoKeNewsActivity;
import com.example.administrator.mynews.adapter.GuoKeRecyclerViewAdapter;
import com.example.administrator.mynews.bean.GuoKeListBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.R.attr.id;
import static com.example.administrator.mynews.Config.Config.API_GUOKE_GET_LIST;

/**
 * Created by Administrator on 2017/5/16.
 */

public class GuoKeFragment extends Fragment implements GuoKeRecyclerViewAdapter.OnGuoKeClickItemListener{
    private RecyclerView recyclerView;
    private List<GuoKeListBean.ResultBean> resultBeanList;
    private ProgressDialog progressDialog;
    private GuoKeRecyclerViewAdapter mAdapter;

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mAdapter.notifyDataSetChanged();
        }
    };


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_guoke,container,false);
        initRecyclerView(view);
        initDate();
        progressDialog=ProgressDialog.show(getActivity(),"提示","正在加载中...",true,false);
        return view;
    }

    private void initDate() {
        OkHttpClient mOkHttpClient=new OkHttpClient();
        Request.Builder requestBuilder = new Request.Builder().url(API_GUOKE_GET_LIST);
        //可以省略，默认是GET请求
        requestBuilder.method("GET",null);
        Request request = requestBuilder.build();
        Call mcall= mOkHttpClient.newCall(request);
        mcall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("liu","访问网络出错");
                progressDialog.dismiss();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    resultBeanList.clear();
                    String content=response.body().string();
                    Log.d("liu","content=="+content);
                    GuoKeListBean guokeListBean=new Gson().fromJson(content,GuoKeListBean.class);
                    Log.d("liu","guokeListBean=="+guokeListBean.getTotal());
                    for (GuoKeListBean.ResultBean resultBean:guokeListBean.getResult()){
                        resultBeanList.add(resultBean);
                    }
                    mHandler.sendEmptyMessage(0);

                }catch (Exception e){
                    Log.d("liu","信息解析错误"+e.getMessage());
                }
                progressDialog.dismiss();
            }
        });
    }


    public void initRecyclerView(View view){
        recyclerView= (RecyclerView) view.findViewById(R.id.guoke_recycler);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        resultBeanList=new ArrayList<>();
        mAdapter=new GuoKeRecyclerViewAdapter(getActivity(),resultBeanList);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnGuoKeClickItemListener(this);
    }

    @Override
    public void onClickItem(int pos) {
        Intent it=new Intent(getActivity(), GuoKeNewsActivity.class);
        it.putExtra("id",resultBeanList.get(pos).getId());
        Log.d("wangxu","Fragment:id==="+id);
        startActivity(it);
    }
}
